<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="版本类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择版本类型">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.APP_MARKET_VERSION_TYPE)"
                     :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否发布" prop="release">
        <el-select v-model="queryParams.release" placeholder="请选择是否发布" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYS_WHETHER_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="更新内容">
        <editor v-model="form.content" :min-height="192"/>
      </el-form-item>
      <el-form-item label="版本号" prop="sn">
        <el-input v-model="form.sn" placeholder="请输入版本号" />
      </el-form-item>
      <el-form-item label="安装包">
        <el-col :span="10">
          <el-upload v-if="!uploaded" ref="upload" :limit="1" accept=".jar" :auto-upload="false" drag
                     :headers="upload.headers" :action="upload.url" :data="upload.data" :disabled="upload.isUploading"
                     :on-change="handleFileChange"
                     :on-progress="handleFileUploadProgress"
                     :on-success="handleFileSuccess">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或 <em>点击上传</em>
            </div>
            <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入 jar 格式文件！</div>
          </el-upload>
          <el-alert
            v-else
            :title="`${this.upload.data.name}上传成功`"
            type="success">
          </el-alert>
          <el-button v-if="!uploaded" style="margin-top: 5px;" type="primary" @click="handleUpload">确认上传</el-button>
          <el-button v-else style="margin-top: 5px;" type="primary" @click="handleReUpload">重新上传</el-button>
        </el-col>
      </el-form-item>
      <el-form-item label="兼容范围" prop="scope">
        <el-input v-model="form.scope" placeholder="请输入兼容范围" />
      </el-form-item>
      <el-form-item label="应用秘钥" prop="secret">
        <el-input v-model="form.secret" placeholder="请输入应用秘钥" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label-width="0" class="text-center mt20">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {getAccessToken} from "@/utils/auth";
import {createMgAppUpdate, getMgAppUpdate, updateMgAppUpdate} from "@/api/product/update";
import {DICT_TYPE, getDictDataLabel, getDictDatas} from "@/utils/dict";
import {isNotEmpty} from "@/utils/ruoyi";
import Editor from '@/components/Editor';
import ImageUpload from "@/components/ImageUpload";
export default {
  name: "renew",
  components:{
    Editor,
    ImageUpload
  },
  data(){
    return{
      id:'',
      // 表单参数
      form: {
        id:'',
        content:'',
        fid:'',
        downloadUrl:'',
        sn:'',
        status:0,
      },
      // 表单校验
      rules: {
        appShort: [{ required: true, message: "应用简称不能为空", trigger: "blur" }],
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
        sn: [{ required: true, message: "版本号不能为空", trigger: "blur" }],
        fid: [{ required: true, message: "安装包不能为空", trigger: "blur" }],
        scope: [{ required: true, message: "兼容范围不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
      //是否已上传
      uploaded:false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getAccessToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/admin-api/mg/app-market-manage/upload", // 请求地址
        data:{
          fid:undefined,
          name:'',
        },
      },
      // 数据字典
      statusDictDatas: getDictDatas(DICT_TYPE.SYS_COMMON_STATUS)
    }
  },
  created() {
    if (this.$route.query.id) {
      this.id =  this.$route.query.id;
      this.getInfo();
    }
  },
  methods:{
    getInfo(){
      getMgAppUpdate(this.id).then(response => {
        this.form = response.data;
        debugger
        if(isNotEmpty(this.form.fid)){
          this.uploaded = true;
        }
      });
    },
    /** 取消按钮 */
    cancel() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: '/product/apps'});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateMgAppUpdate(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
          });
          return;
        }
        // 添加的提交
        createMgAppUpdate(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
        });
      });
    },
    /** 处理上传的文件发生变化 */
    handleFileChange(file, fileList) {
      this.upload.data.path = file.name;
    },
    /** 处理文件上传中 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true; // 禁止修改
    },
    /** 文件上传成功处理 */
    handleFileSuccess(response, file, fileList) {
      // 清理
      this.uploaded = true;
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.form.downloadUrl = response.data.url;
      debugger
      this.form.fid = response.data.fid;
      // 提示成功，并刷新
      this.$modal.msgSuccess("上传成功");
    },
    handleUpload(){
      this.$refs.upload.submit();
    },
    handleReUpload(){
      this.uploaded = false;
    }
  }
}
</script>

<style scoped>

</style>
